#include <reg51.h>  	
#include"lcd.h"

#define PWD_A 5
#define PWD_B 4
#define PWD_C 8

sbit rel1=P3^0;
sbit rel2=P3^1;
sbit rel3=P3^2;

sbit sw1=P1^4;
sbit sw2=P1^5;
sbit sw3=P1^6;

sbit buz=P1^7;

sbit A_Red=P2^0;
sbit A_Green=P2^1;
sbit B_Red=P2^2;
sbit B_Green=P2^3;
sbit C_Red=P2^4;
sbit C_Green=P2^5;

unsigned char a[4][3]={'1','2','3','4',
							  '5','6','7','8',
							  '9','0','a','b'};
							  
unsigned char val[] = {'0','1','2','3','4','5','6','7','8','9','*','0','#'};

						   
unsigned char GSM[3][10] = {'8','5','2','5','0','8','5','6','5','5',
							'8','5','2','5','0','8','5','6','5','5',
							'8','5','2','5','0','8','5','6','5','5'};
							
unsigned char tripa=1,tripadata=0,tripb=1,tripbdata=0,tripc=1,tripcdata=0,tripdata=0,r=0,c=0,ch=0,sw=0;
unsigned int ret=0;

void tx(unsigned char a)
{
      	SBUF = a;
		 	while(TI == 0);
			TI = 0;
}
 

//*********************************************************************************
//                SERIAL INIT_TX_RX
//*********************************************************************************
 
void baud_init ()
	{	
		TMOD = 0x20;
		TH1 = 0xFD;
		SCON = 0x50;
		TR1 = 1;
	}
	

//*******************************************************************************

void buzzer(unsigned char loop, int tone)
{
   unsigned char beep;
   for(beep=0;beep<loop;beep++)
   {
      buz=1;
      LCD_delay(tone);
      buz=0;
      LCD_delay(tone);
   }
}


unsigned char kb()
  {
  unsigned char li=0,lj=0xee,lk,lr=45;
  for(li=0;li<4;li++)
  {
  P1=lj;
  lk=P3;
     LCD_delay(15);
  lk &= 0x38; 
  if(lk!=0x38)
  {
  if(lk==0x30)
  lr=0+(li*3);
  if(lk==0x28)
  lr=1+(li*3);
  if(lk==0x18)
  lr=2+(li*3);
 }
  lj = (lj<<1)+1;
  }
if(lr==10)
{
lr=0;
}
else
{
 lr=lr+1;
} 
  return(lr);
}
    

//********************************************************************************

unsigned char eid()
{
	unsigned char i=0, flag=1;
		LCD_row2();
		while(1)
		{
      ch=kb(); 		 	
      if(ch!=45)
      	{
      	LCD_putc(val[ch]); 
      	LCD_delay(5000);
      	LCD_delay(5000);
      	break;
      	}       
      }
      return(ch);
}


//********************************************************************************

unsigned char passwd(unsigned char j)
{
	unsigned char flag=1;
	LCD_clear();
  	LCD_row1();
  	LCD_puts("Enter Password:");
  	LCD_delay(100);
	LCD_row2();

		while(1)
		{
		ch=45;
		ch=kb(); 		 	
		if(ch!=45)
		{
		 if(sw==1 && ch==PWD_A){flag=0;}
		 if(sw==2 && ch==PWD_B){flag=0;}
		 if(sw==3 && ch==PWD_C){flag=0;}
      		break;}
    }
	return flag;
}

//***********************************************************************************
	
void onbar()
{
  	LCD_clear();
  	LCD_row1();
  	LCD_puts("ON-BAR FEEDERS"); 
	LCD_delay(250);
  	LCD_row2();
	
	if(tripa==1)
	{
		LCD_puts(" A"); 
	}
	if(tripb==1)
	{
		LCD_puts(" B"); 
	}
	if(tripc==1)
	{
		LCD_puts(" C"); 
	}
  	
	LCD_delay(1000); 
}


 void send_string(char code *ptr)
{
	while (*ptr != 0)
	{
		SBUF = *ptr;
		ptr++;
		while (TI == 0);
		TI = 0;
		LCD_delay(1);
	}

}


            
void sendwarn(unsigned char a,unsigned char b)
{
unsigned char q;
  		send_string("at+cmgf=1\n");   
      tx(0x0d);
      LCD_delay(100);
	   send_string("at+cmgs=\"+91");
	   for(q=0;q<10;q++)
	   {
	   	tx(GSM[b-1][q]);
	   }
	   send_string("\"\n");
      tx(0x0d);
      tx(0x0a);
      LCD_delay(100);
      
      tx('A'+(a-1));
	   send_string(" Try to open \n");
      tx('A'+(b-1));
      tx(0x0d);
      tx(0x0a);
      LCD_delay(5000);
      
      
  		send_string("at+cmgf=1\n");   
      tx(0x0d);
      LCD_delay(100);
	   send_string("at+cmgs=\"+");
	   LCD_delay(10);
 	   send_string("91");
	   LCD_delay(10);
	   for(q=0;q<10;q++)
	   {
	   	tx(GSM[a-1][q]);
	   }
	   send_string("\"\n");
      tx(0x0d);
      tx(0x0a);
      LCD_delay(100);
      
      tx('A'+(a-1));
	   send_string(" Try to open \n");
      tx('A'+(b-1));
      tx(0x0d);
      tx(0x0a);
      LCD_delay(100);
           
}

void sendss(unsigned char a)
{
unsigned char q;
 	send_string("at+cmgf=1\n");   
      tx(0x0d);
      LCD_delay(100);
	   send_string("at+cmgs=\"+91");
	   for(q=0;q<10;q++)
	   {
	   	tx(GSM[a-1][q]);
	   }
	   send_string("\"\n");
      tx(0x0d);
      tx(0x0a);
      LCD_delay(100);
      
      tx('A'+(a-1));
	   send_string(" Is OPERATED \n");
      tx(0x0d);
      tx(0x0a);
      LCD_delay(1000);
}


void sendpw(unsigned char a,unsigned char ab)
{

unsigned char q;
 	send_string("at+cmgf=1\n");   
      tx(0x0d);
      LCD_delay(100);
	   send_string("at+cmgs=\"+91");
	   for(q=0;q<10;q++)
	   {
	   	tx(GSM[a-1][q]);
	   }
	   send_string("\"\n");
      tx(0x0d);
      tx(0x0a);
      LCD_delay(100);
      tx(0x30 + (ab/10));
      tx(0x30 + (ab%10));
		tx(' ');
	   send_string(" Is OTP for operating ");
      tx('A'+(a-1));
      tx(0x0d);
      tx(0x0a);
      LCD_delay(1000);
}
		
		

//**********************************************************************************

void trip()
{
unsigned char tempabc=0,ch1,ch2,chk,testwer=1;
if(tripa==0 && tripb==0 && tripc==0)
{
	LCD_clear();
  	LCD_row1();
  	LCD_puts(" ALL FEEDERS"); 
	LCD_delay(5);
  	LCD_row2();
  	LCD_puts(" ARE ON-BAR");  
	LCD_delay(5);
	testwer=0;
}

if(testwer==1)
{
tttt: 	LCD_clear();
  	LCD_row1();
  	LCD_puts("Enter to ON-BAR"); 
	LCD_delay(5);
  	LCD_row2();
  		
	if(tripa==0)
	{
		LCD_puts(" 1.A"); 
	}
	if(tripb==0)
	{
		LCD_puts(" 2.B"); 
	}
	if(tripc==0)
	{
		LCD_puts(" 3.C"); 
	}
  	
	LCD_delay(50);  
	
  	ch1=45;
	while((ch1!=1)&&(ch1!=2)&&(ch1!=3))  
   	{ch1=kb();}
	
  	LCD_clear();
  	LCD_row1();
  	LCD_puts("Want to On-BAR?"); 
  	LCD_row2();
	LCD_putc('A'+(ch1-1)); 
	LCD_delay(500);
	
  	ch2=75;
	while((ch2!=1)&&(ch2!=2)&&(ch2!=0))  
   	{ch2=kb();}
	
	if(ch2==0)
	{
	if(ch1==1)
	{
		if(tripa==0)
		{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("Enter OTP for A"); 
 	 	LCD_row2();
		LCD_delay(10);
		
		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}

   		LCD_putc(val[chk]);
		LCD_delay(1000);
		tempabc=0;
		tempabc=chk*10;
   		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}

		LCD_putc(val[chk]);
		LCD_delay(1000);		
		chk=tempabc+chk;


		if(chk==tripadata)
		{
		A_Red= 1;
		A_Green= 0;
		sendss(1);
		tripa=1;
		}	
		else
		{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("A Malfunctioned"); 
 	 	LCD_row2();
   		buzzer(15,50);			
		LCD_delay(10);			
		sendwarn(1,sw);
		}
		}
		else
		{ 
		  	LCD_clear();
  		LCD_row1();
  		LCD_puts("A is Already"); 
 	 	LCD_row2();
		LCD_puts(" in On-BAR "); 	
		}
	 }
	 

	if(ch1==2)
	{
		if(tripb==0)
		{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("Enter OTP for B"); 
 	 	LCD_row2();
		LCD_delay(10);
		

		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}
   	
		LCD_putc(val[chk]);
		LCD_delay(1000);
		tempabc=0;
		tempabc=chk*10;
   		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}

		LCD_putc(val[chk]);
		LCD_delay(1000);		
		chk=tempabc+chk;

		if(chk==tripbdata)
		{
		B_Red= 1;
		B_Green= 0;
		sendss(2);
		tripb=1;
		}	
		else
		{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("B Malfunctioned"); 
 	 	LCD_row2();
   		buzzer(15,50);			
		LCD_delay(10);			
		sendwarn(2,sw);
		}
		}
		else
		{ 
		LCD_clear();
  		LCD_row1();
  		LCD_puts("B is Already"); 
 	 	LCD_row2();
		LCD_puts(" in On-BAR ");		
		}
	 }

	if(ch1==3)
	{
		if(tripc==0)
		{
		
		LCD_clear();
  		LCD_row1();
  		LCD_puts("Enter OTP for C"); 
 	 	LCD_row2();
		LCD_delay(10);
		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}
		LCD_putc(val[chk]);
		LCD_delay(1000);
		tempabc=0;
		tempabc=chk*10;
   		chk=45;
		while(1)
		{
      		chk=kb(); 		 	
      		if(chk!=45)
      		{
      		break;
      		}
		}

		LCD_putc(val[chk]);
		LCD_delay(1000);		
		chk=tempabc+chk;

		if(chk==tripcdata)
		{
		C_Red= 1;
		C_Green= 0;
		sendss(3);
		tripc=1;
		}	
		else
		{		
		LCD_clear();
  		LCD_row1();
  		LCD_puts("C Malfunctioned"); 
 	 	LCD_row2();
   		buzzer(15,50);			
		LCD_delay(10);			
		sendwarn(3,sw);
		}	
		}
		else
		{ 
		LCD_clear();
  		LCD_row1();
  		LCD_puts("C is Already"); 
 	 	LCD_row2();
		LCD_puts(" in On-BAR ");	
		}
	 }
	 
	 }
	else
	{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("Enter Valid"); 
 	 	LCD_row2();
		LCD_puts(" Number ");
		LCD_delay(3000);
	goto tttt;		
	}
}
}

//***********************************************************************************
	
void trippit()
{
unsigned char ch1,ch2,testwer=1;

if(tripa==0 && tripb==0 && tripc==0)
{
	LCD_clear();
  	LCD_row1();
  	LCD_puts(" ALL FEEDERS"); 
	LCD_delay(5);
  	LCD_row2();
  	LCD_puts(" ARE TRIPPED");  
	LCD_delay(5);
	testwer=0;
}

if(testwer==1)
{
tttt: 	LCD_clear();
  	LCD_row1();
  	LCD_puts("Want to Trip"); 
	LCD_delay(5);
  	LCD_row2();
  		
	if(tripa==1)
	{
		LCD_puts(" 1.A"); 
	}
	if(tripb==1)
	{
		LCD_puts(" 2.B"); 
	}
	if(tripc==1)
	{
		LCD_puts(" 3.C"); 
	}
  	
	LCD_delay(50); 
	
  	ch1=45;
	while((ch1!=1)&&(ch1!=2)&&(ch1!=3))  
   	{ch1=kb();}
	
  	LCD_clear();
  	LCD_row1();
  	LCD_puts("Are You Sure"); 
  	LCD_row2();
	LCD_puts(" to Trip "); 
	LCD_putc('A'+(ch1-1)); 
	LCD_delay(50);
	
  	ch2=75;
	while((ch2!=1)&&(ch2!=2)&&(ch2!=0))  
   	{ch2=kb();}
	
	if(ch2==0)
	{
	if(ch1==1)
	{
		if(tripa==1)
		{
		A_Red= 0;
		A_Green= 1;
		tripa=0;

		LCD_clear();
  		LCD_row1();
  		LCD_puts("Feeder A Tripped "); 
 	 	LCD_row2();
		LCD_puts(" successfully "); 
		tripadata=tripdata;
		sendpw(1,tripadata);
		}
		else
		{ 
		LCD_clear();
  		LCD_row1();
  		LCD_puts("A is Already"); 
 	 	LCD_row2();
		LCD_puts(" in Trip "); 
		}
	 }
	 	 

	if(ch1==2)
	{
		if(tripb==1)
		{
		B_Red= 0;
		B_Green= 1;
		tripb=0;

		LCD_clear();
  		LCD_row1();
  		LCD_puts("Feeder B Tripped "); 
 	 	LCD_row2();
		LCD_puts(" successfully "); 

		tripbdata=tripdata;
		sendpw(2,tripdata);	
		}
		else
		{ 
		  	LCD_clear();
  		LCD_row1();
  		LCD_puts("B is Already"); 
 	 	LCD_row2();
		LCD_puts(" in Trip "); 	
		}
	 }

	if(ch1==3)
	{
		if(tripc==1)
		{
		C_Red= 0;
		C_Green= 1;
		tripc=0;

		LCD_clear();
  		LCD_row1();
  		LCD_puts("Feeder C Tripped "); 
 	 	LCD_row2();
		LCD_puts(" successfully "); 

		tripcdata=tripdata;
		sendpw(3,tripdata);	
		}
		else
		{ 
		  	LCD_clear();
  		LCD_row1();
  		LCD_puts("c is Already"); 
 	 	LCD_row2();
		LCD_puts(" in Trip "); 		
		}
	 }
	 }
	else
	{

		LCD_clear();
  		LCD_row1();
  		LCD_puts("Enter Valid"); 
 	 	LCD_row2();
		LCD_puts(" Number ");
		LCD_delay(3000);
	goto tttt;		
	}
}
}

//***********************************************************************************
	
void main( void )
{       
	unsigned char test;       
	LCD_init();
  	baud_init ();
  	LCD_row1();
  	LCD_puts("Enter EMP ID:");
  			LCD_delay(100); 
  	P2=0x55;
   		buzzer(3,1000);			
  	while(1)
  	{
  	    sw=eid(); 		 	
   if(sw<3)
   {
   test=passwd(sw);
   	if(test==0)
   	{
  			LCD_clear();
  			LCD_row1();
  			LCD_puts("1.ON-BAR"); 
  			LCD_row2();
  			LCD_puts("2.TRIPPED");  
  			LCD_delay(50); 
  			
  			ch=45;
				while((ch!=1)&&(ch!=2))  
   			{ch=kb();
   			}
   			
   		if(ch==1)
   		{
   		LCD_clear();
   		onbar();
			trippit();  
  			LCD_delay(5000);  	  
		
   		}
   		else
   		{LCD_clear();
   		trip();  
  			LCD_delay(5000);  	  
   		}   	
   	}
   	else
   	{
  			LCD_clear();
  			LCD_row1();
  			LCD_puts("Enter Valid PWD");   
  			LCD_delay(5000);  	  	
   	}
   }
   
   else
   {
  	LCD_clear();
  	LCD_row1();
  	LCD_puts("Enter Valid ID");   
  	LCD_delay(5000);  
  	}  
  	LCD_clear();
  	LCD_row1(); 
  	LCD_puts("Enter EMP ID:");
  	sw=48;
  	if(tripdata==100) tripdata=0; 

	tripdata++;

	if(sw1==1)
{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("A Malfunctioned");
   		buzzer(15,50);			
		sendwarn(1,1);
		LCD_delay(1000);
}
	
	if(sw2==1)
{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("B Malfunctioned"); 
 	 	LCD_row2();
   		buzzer(15,50);						
		sendwarn(2,2);
		LCD_delay(1000);
}

	if(sw3==1)
{
		LCD_clear();
  		LCD_row1();
  		LCD_puts("C Malfunctioned"); 
 	 	LCD_row2();
   		buzzer(15,50);						
		sendwarn(3,3);
		LCD_delay(1000);
}
  	}   
}
